package org.minuframework.jdbc;

import java.io.File;
import java.util.Properties;

import org.minuframework.file.FileUtils;
import org.minuframework.utils.ParamUtils;

public class JdbcProperties {
    
    public static String DEFAULT_JDBC_FILE        = "properties/jdbc.properties";
    public static String DEFAULT_JDBC_DRIVER      = "driver";
    public static String DEFAULT_JDBC_URL         = "url";
    public static String DEFAULT_JDBC_USERNAME    = "username";
    public static String DEFAULT_JDBC_PASSWORD    = "password";
    public static String DEFAULT_JDBC_INITIALSIZE = "initialSize";
    public static String DEFAULT_JDBC_MINIDLE     = "minIdle";
    public static String DEFAULT_JDBC_MAXACTIVE   = "maxActive";
    public static String DEFAULT_JDBC_DATABASE    = "database";
    
    protected Properties properties;
    
    public JdbcProperties() {
        this(null);
    }
    
    public JdbcProperties(Properties properties) {
        super();
        this.properties = properties;
    }
    
    public Properties getParamerRepository() {
        if (properties == null) {
            String pathname = ParamUtils.getPathName(DEFAULT_JDBC_FILE);
            properties = FileUtils.load(new File(pathname));
        }
        return properties;
    }
    
    public String getDriver() {
        return getParamerRepository().getProperty(DEFAULT_JDBC_DRIVER);
    }
    
    public String getUrl() {
        return getParamerRepository().getProperty(DEFAULT_JDBC_URL);
    }
    
    public String getUsername() {
        return getParamerRepository().getProperty(DEFAULT_JDBC_USERNAME);
    }
    
    public String getPassword() {
        return getParamerRepository().getProperty(DEFAULT_JDBC_PASSWORD);
    }
    
    public int getInitialSize() {
        if (getParamerRepository().contains(DEFAULT_JDBC_INITIALSIZE)) {
            return Integer.parseInt(getParamerRepository().getProperty(DEFAULT_JDBC_INITIALSIZE));
        } else {
            return 0;
        }
    }
    
    public int getMinIdle() {
        if (getParamerRepository().contains(DEFAULT_JDBC_MINIDLE)) {
            return Integer.parseInt(getParamerRepository().getProperty(DEFAULT_JDBC_MINIDLE));
        } else {
            return 0;
        }
    }
    
    public int getMaxActive() {
        if (getParamerRepository().contains(DEFAULT_JDBC_MAXACTIVE)) {
            return Integer.parseInt(getParamerRepository().getProperty(DEFAULT_JDBC_MAXACTIVE));
        } else {
            return 0;
        }
    }
    
    public String getDatabase() {
        return getParamerRepository().getProperty(DEFAULT_JDBC_DATABASE);
    }
    
    public Properties getProperties() {
        return properties;
    }
    
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    
    public boolean isEmpty() {
        return getParamerRepository().isEmpty();
    }
    
    @Override
    public String toString() {
        return "JdbcProperties [" + properties + "]";
    }
    
    public static void main(String[] args) {
        JdbcProperties properties = new JdbcProperties();
        properties.getParamerRepository();
        System.out.println(properties.toString());
    }
    
}
